/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaces;

import datos.Administrador;
import datos.Coordinador;
import datos.Operador;
import datos.ResponsableEntidad;
import exceptions.UsuarioException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author miguesaca
 */
public class Panel_Login extends Panel_T {

    private String cedula, contrasena, ClaseUsuario;
    private Operador operador;
    private Coordinador coordinador;
    private Administrador administrador;
    private Ventana ventana;

    public Panel_Login(Ventana ventana) {
        operador = new Operador();
        coordinador = new Coordinador();
        administrador = new Administrador();
        this.ventana = ventana;
        ClaseUsuario = null;
        initComponents();
    }

    public String getCedula() {
        return cedula;
    }

    public String getClaseUsuario() {
        return ClaseUsuario;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        text_NombreUsuario = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        text_Contrasena = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        boton_Logear = new javax.swing.JButton();

        setBackground(new java.awt.Color(51, 51, 51));

        jLabel1.setText("Nombre de Usuario");

        jLabel2.setText("Contrasena");

        boton_Logear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/icon_login1.png"))); // NOI18N
        boton_Logear.setBorderPainted(false);
        boton_Logear.setContentAreaFilled(false);
        boton_Logear.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        boton_Logear.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton_Logear.setIconTextGap(-3);
        boton_Logear.setPreferredSize(new java.awt.Dimension(50, 50));
        boton_Logear.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/icon_login2.png"))); // NOI18N
        boton_Logear.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/icon_login0.png"))); // NOI18N
        boton_Logear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_LogearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(218, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(boton_Logear, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(text_Contrasena, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                            .addComponent(text_NombreUsuario))))
                .addGap(300, 300, 300))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(167, 167, 167)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(text_NombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(text_Contrasena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boton_Logear, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(207, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void boton_LogearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_LogearActionPerformed
        ClaseUsuario = null;
        cedula = text_NombreUsuario.getText();
        contrasena = text_Contrasena.getText();
        comoOperador();
        contrasena = null;
        text_Contrasena.setText("");
    }//GEN-LAST:event_boton_LogearActionPerformed
    private void comoOperador() {
        try {
            operador = ventana.getGestorOperadoras().buscar(cedula);
            if (operador != null) {
                if (contrasena.equalsIgnoreCase(operador.getContrasena())) {
                    this.setVisible(false);
                    ClaseUsuario = operador.getClass().getName();
                } else {
                    JOptionPane.showMessageDialog(null, "Contrasena Invalida");
                }
            } else {
                comoCoordinador();
            }
        } catch (UsuarioException ex) {
            comoCoordinador();
        }
    }

    private void comoCoordinador() {
        try {
            coordinador = ventana.getGestorCoordinadores().buscar(cedula);
            if (coordinador != null) {
                if (contrasena.equalsIgnoreCase(coordinador.getContrasena())) {
                    this.setVisible(false);
                    ClaseUsuario = coordinador.getClass().getName();
                } else {
                    JOptionPane.showMessageDialog(null, "Contrasena Invalida");
                }
            } else {
                comoAdministrador();
            }
        } catch (UsuarioException ex) {
            comoAdministrador();
        }
    }

    private void comoAdministrador() {
        try {
            administrador = ventana.getGestorAdministradores().buscar(cedula);
            if (administrador != null) {
                if (contrasena.equalsIgnoreCase(administrador.getContrasena())) {
                    this.setVisible(false);
                    ClaseUsuario = administrador.getClass().getName();
                } else {
                    JOptionPane.showMessageDialog(null, "Contrasena Invalida");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Usuario   Inexistente");
            }
        } catch (UsuarioException ex) {
            JOptionPane.showMessageDialog(null, "Usuario   Inexistente");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_Logear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField text_Contrasena;
    private javax.swing.JTextField text_NombreUsuario;
    // End of variables declaration//GEN-END:variables
}
